/**
 * Copyright (C) 2019 Baidu, Inc. All Rights Reserved.
 */
package com.bailiban.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bailiban.dao.StadiaDao;
import com.bailiban.pojo.Stadia;
import com.bailiban.utils.JDBCUtils;
import com.bailiban.utils.ResultMsg;

/**
 * @author zhulang
 * @version 创建时间：2019年9月27日 上午10:57:33
 */
public class StadiaDaoImpl implements StadiaDao {
    private static Connection connection = null;
    private static PreparedStatement statement = null;
    private static ResultSet rs = null;

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.StadiaDao#findAllStadia()
     */
    @Override
    public List<Stadia> findAllStadia() {
        List<Stadia> list = new ArrayList<Stadia>();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT sid,sname,address,description from tb_stadia";
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                Stadia stadia = new Stadia();
                stadia.setSid(rs.getInt("sid"));
                stadia.setSname(rs.getString("sname"));
                stadia.setAddress(rs.getString("address"));
                stadia.setDescription(rs.getString("description"));
                list.add(stadia);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.StadiaDao#findStadiaBySid(int)
     */
    @Override
    public Stadia findStadiaBySid(int sid) {
        Stadia stadia = new Stadia();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT sid,sname,address,description from tb_stadia where sid=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, sid);
            rs = statement.executeQuery();
            while (rs.next()) {

                stadia.setSid(rs.getInt("sid"));
                stadia.setSname(rs.getString("sname"));
                stadia.setAddress(rs.getString("address"));
                stadia.setDescription(rs.getString("description"));

            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return stadia;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.StadiaDao#deleteStadiaBySid(int)
     */
    @Override
    public ResultMsg deleteStadiaBySid(int sid) {
        ResultMsg result = new ResultMsg();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "DELETE from tb_stadia WHERE sid=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, sid);
            int update = statement.executeUpdate();
            if (update > 0) {
                result.setFlag(true);
            } else {
                result.setFlag(false);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.StadiaDao#addStadia(com.bailiban.pojo.Stadia)
     */
    @Override
    public ResultMsg addStadia(Stadia stadia) {
        ResultMsg result = new ResultMsg();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "INSERT INTO tb_stadia(sname,address,description) VALUES(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, stadia.getSname());
            statement.setString(2, stadia.getAddress());
            statement.setString(3, stadia.getDescription());

            int update = statement.executeUpdate();
            if (update > 0) {
                result.setFlag(true);
            } else {
                result.setFlag(false);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.StadiaDao#updateStadia(com.bailiban.pojo.Stadia)
     */
    @Override
    public ResultMsg updateStadia(Stadia stadia) {
        ResultMsg result = new ResultMsg();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "UPDATE tb_stadia SET sname=?,address=?,description=? WHERE sid=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, stadia.getSname());
            statement.setString(2, stadia.getAddress());
            statement.setString(3, stadia.getDescription());
            statement.setInt(4, stadia.getSid());
            int update = statement.executeUpdate();
            if (update > 0) {
                result.setFlag(true);
            } else {
                result.setFlag(false);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

}
